package day17.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
socket 网络编程
TCP
UDP
 */
public class Test {
    public static void main(String[] args) {
        // 创建一个基于TCP协议的服务端对象
        System.out.print("tcp服务已经启动，等待有缘人连接...\n");
        try(
                OutputStream os = new FileOutputStream("d:\\mytemp\\abc.jpg");
                ServerSocket serverSocket = new ServerSocket(9527);
                Socket socket = serverSocket.accept();
        ) {
            System.out.println("有人连上了，这个人是：" + socket);
            BufferedInputStream is = new BufferedInputStream(socket.getInputStream(), 1024 * 1024 * 50);
            byte[] data = new byte[1024 * 1024];
            int len = 0;
            while ((len = is.read(data)) != -1) {
                os.write(data, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
